/****************************************************************
 * *************************README*******************************
 * This code is my GD32F330 as an example, according to the GD32 
 * official standard library to write their own library.
 * Open source in gitee.address:https://gitee.com/FeelingforYou/my-own-library
 * time:2024/11/20
 * author: pilot
*****************************************************************/
#ifndef GPIO_H
#define GPIO_H

#include "gd32f3x0_libopt.h"

typedef enum
{
    A0 = 32* 0, A1 ,    A2 ,    A3 ,    A4 ,    A5 ,    A6 ,    A7 ,
    A8 ,        A9 ,    A10,    A11,    A12,    A13,    A14,    A15,

    B0 = 32* 1, B1 ,    B2 ,    B3 ,    B4 ,    B5 ,    B6 ,    B7 ,
    B8 ,        B9 ,    B10,    B11,    B12,    B13,    B14,    B15,

    C0 = 32* 2, C1 ,    C2 ,    C3 ,    C4 ,    C5 ,    C6 ,    C7 ,
    C8 ,        C9 ,    C10,    C11,    C12,    C13,    C14,    C15,

    D0 = 32* 3, D1 ,    D2 ,    D3 ,    D4 ,    D5 ,    D6 ,    D7 ,
    D8 ,        D9 ,    D10,    D11,    D12,    D13,    D14,    D15,

    F0 = 32* 4, F1 ,    F2 ,    F3 ,    F4 ,    F5 ,    F6 ,    F7 ,
    F8 ,        F9 ,    F10,    F11,    F12,    F13,    F14,    F15,

}gpio_pin_enum;

typedef enum               // io direction (in or out)
{
    GPI = 0,               // io in
    GPO = 1,               // io out
}gpio_dir_enum;

typedef enum               // io electric level
{
    GPIO_LOW =  0,         // io low power
    GPIO_HIGH = 1,         // io high power
}gpio_level_enum;

typedef enum               // io input or out mode
{
    GPI_NONE_IN    ,       // io floating in
    GPI_PULL_UP    ,       // io pull up
    GPI_PULL_DOWN  ,       // io pull down

    GPO_PUSH_PULL  ,       // io Push-pull output
    GPO_OPEN_DTAIN ,       // io open-dtain output
}gpio_mode_enum;

typedef enum
{
    GPIO_AF0,
    GPIO_AF1,
    GPIO_AF2,
    GPIO_AF3,
    GPIO_AF4,
    GPIO_AF5,
    GPIO_AF6,
    GPIO_AF7,
}gpio_af_enum;


#define io_group(pin)    (pin >> 5) 
#define io_pin(pin)      (pin & 0x1F) 


/*****************************Internal function**************************************/
static uint32_t io_group_get(gpio_pin_enum pin);
static uint32_t af_group_get(gpio_af_enum af);
/************************************************************************************/

extern void gpio_init(gpio_pin_enum pin, gpio_dir_enum dir, uint8_t dat, gpio_mode_enum pinmode);
FlagStatus io_read(gpio_pin_enum pin);
extern void io_write(gpio_pin_enum pin, FlagStatus dat);
extern void io_toggle(gpio_pin_enum pin);
extern void afio_init(gpio_pin_enum pin, gpio_af_enum af, gpio_mode_enum pinmode);
#endif
